<?php

namespace backend\models\file;

use backend\utils\AppUtil;
use Yii;

/**
 * This is the model class for table "att_school_files".
 *
 * @property string $uuid 主键
 * @property string $attachment_id 附件id|attachment
 * @property int $type '类型:1 审计报告(Word);2 审计报告(PDF);
 * @property int $status 状态:1 使用;2 注销
 * @property string $create_time 添加时间
 * @property string $update_time 修改时间
 */
class AttSchoolFiles extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'att_school_files';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uuid', 'attachment_id', 'type'], 'required'],
            [['type', 'status'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['uuid', 'attachment_id'], 'string', 'max' => 36],
            [['uuid'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'uuid' => '主键',
            'attachment_id' => '附件id|attachment',
            'type' => '类型:1 绩效报告',
            'status' => '状态:1 使用;2 注销',
            'create_time' => '添加时间',
            'update_time' => '修改时间',
        ];
    }

    //类型:1 审计报告(Word);2 审计报告(PDF) ; 3 推荐意见盖章扫描件
    const TYPE_WORD = 1;
    const TYPE_PDF = 2;
    const TYPE_FILE_3 = 3;

    /**
     * @desc
     * @return string
     * @Date: 2024/11/1
     */
    static function get_table_field($type): string
    {
        return self::tableName() . ".attachment_id.{$type}";
    }

    static function get_model_by_type($type)
    {
        return AttSchoolFiles::findOne([
            'type' => $type,
            'status' => AppUtil::STATUS_USE,
        ]);
    }

    /**
     * @desc
     * @return array|mixed|\yii\db\ActiveRecord
     * @Date: 2024/11/1
     */
    static function get_file_by_type($type)
    {
        $model = self::get_model_by_type($type);
        $attachment_id = $model ? $model->attachment_id : '';

        $file = AttExtend::get_file_by_attachment_id($attachment_id);

        return $file ? $file[0] : [];
    }
}
